package com.iraeta.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.entity.Iraorderwuliu;
import com.iraeta.entity.Iraorderwuliustepspeed;
import com.iraeta.entity.vo.IraOrderWuLiuProductVo;
import com.iraeta.entity.vo.IraorderwuliuVo;
import com.iraeta.service.iraorder.IraorderwuliuService;
import com.iraeta.service.iraorder.IraorderwuliuproductService;
import com.iraeta.service.iraorder.IraorderwuliustepspeedService;
import com.iraeta.service.sys.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
 * 主订单的物流订单维护功能
 * */
@RestController(value = "IraOrderWuLiuController")
@RequestMapping("emp/orderwuliu")
public class IraOrderWuLiuController extends BaseController{
    @Resource
    IraorderwuliuService iraorderwuliuService;
    @Resource
    IraorderwuliustepspeedService iraorderwuliustepspeedService;
    @Resource
    IraorderwuliuproductService iraorderwuliuproductService;
    @Resource
    UserService userService;

    /*物流订单管理*/
    @RequestMapping("pagewuliu")
    public String pageWuLiu(@RequestBody JSONObject jo){
        int orderId = jo.getIntValue("orderId");
        this.pageSet(jo);
        List<Iraorderwuliu> iraorderwuliuList = iraorderwuliuService.listIraorderwuliuByOrderId(orderId);
        List<IraorderwuliuVo> iraorderwuliuVoList = new ArrayList<>();
        for (Iraorderwuliu iraorderwuliu : iraorderwuliuList) {
            IraorderwuliuVo iraorderwuliuVo = new IraorderwuliuVo();
            BeanUtil.copyProperties(iraorderwuliu, iraorderwuliuVo);
            List<Iraorderwuliustepspeed> iraorderwuliustepspeedList = iraorderwuliustepspeedService.listIraorderwuliustepspeed(iraorderwuliu.getId());
            iraorderwuliuVo.setStepList(iraorderwuliustepspeedList);
            List<IraOrderWuLiuProductVo> iraOrderWuLiuProductVoList  = iraorderwuliuproductService.listIraorderwuliuproduct(orderId, iraorderwuliu.getId());
            iraorderwuliuVo.setProductList(iraOrderWuLiuProductVoList);
            iraorderwuliuVoList.add(iraorderwuliuVo);
        }
        PageInfo<IraorderwuliuVo> IraorderwuliuVoPageInfo = this.getPageInfo(iraorderwuliuVoList);
        return this.success(IraorderwuliuVoPageInfo);
    }
    @RequestMapping("getwuliu")
    public String getWuLiu(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Iraorderwuliu iraorderwuliu = iraorderwuliuService.getIraorderwuliu(id);
        return this.success(iraorderwuliu);
    }
    @RequestMapping("addeditwuliu")
    public String addEditWuLiu(@RequestBody JSONObject paramJb){
        int wuLiuId = paramJb.getIntValue("id");
        Iraorderwuliu iraorderwuliu = wuLiuId == 0 ? new Iraorderwuliu() : iraorderwuliuService.getIraorderwuliu(wuLiuId);
        this.mapJsonToObj(paramJb, iraorderwuliu);
        if(iraorderwuliu.getCompanyuser() > 0){
            iraorderwuliu.setCompanyname(userService.getUser(iraorderwuliu.getCompanyuser()).getName());
        }
        JSONArray stepList = paramJb.getJSONArray("stepList");
        if(wuLiuId == 0){
            iraorderwuliuService.addIraorderwuliu(iraorderwuliu, stepList);
        }else {
            iraorderwuliuService.updateIraorderwuliu(iraorderwuliu, stepList);
        }
        return this.success(iraorderwuliu);
    }
    @RequestMapping("deletewuliu")
    public String deleteWuLiu(@RequestBody JSONObject paramJb){
        int wuLiuId = paramJb.getIntValue("id");
        iraorderwuliuService.deleteIraorderwuliu(wuLiuId);
        return this.success();
    }

    /*2 物流订单的产品货物管理*/
    @RequestMapping("listwuliuproduct")
    public String listWuLiuProduct(@RequestBody JSONObject jo){
        int orderId = jo.getIntValue("orderId");
        int wuLiuId = jo.getIntValue("wuLiuId");
        List<IraOrderWuLiuProductVo> iraOrderWuLiuProductVoList = iraorderwuliuproductService.listIraorderwuliuproduct(orderId, wuLiuId);
        return this.success(iraOrderWuLiuProductVoList);
    }
    /*2 更新物流单的产品货物*/
    @RequestMapping("savewuliuproduct")
    public String saveWuLiuProduct(@RequestBody JSONObject jo){
        int wuLiuId = jo.getIntValue("wuLiuId");
        JSONArray wuLiuProductList = jo.getJSONArray("wuLiuProductList");
        iraorderwuliuproductService.updateIraorderwuliuproduct(wuLiuId, wuLiuProductList);
        return this.success();
    }

    /*3 物流订单的步骤管理*/
    @RequestMapping("listwuliustep")
    public String listWuLiuStep(@RequestBody JSONObject jo){
        int wuLiuId = jo.getIntValue("wuLiuId");
        List<Iraorderwuliustepspeed> iraorderwuliustepspeedList = iraorderwuliustepspeedService.listIraorderwuliustepspeed(wuLiuId);
        return this.success(iraorderwuliustepspeedList);
    }
    /*3 删除物流单的步骤*/
    @RequestMapping("deletewuliustop")
    public String deleteOrdrStep(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        this.iraorderwuliustepspeedService.deleteIraorderwuliustepspeed(id);
        return this.success();
    }
    /*3 更新物流单的步骤*/
    @RequestMapping("updatewuliustep")
    public String updateWuLiuStep(@RequestBody JSONObject jo){
        JSONArray stepList = jo.getJSONArray("stepList");
        iraorderwuliustepspeedService.updateIraorderwuliustepspeed(stepList);
        return this.success();
    }

    /*4 物流用户查看分配给自己的物流订单*/
    @RequestMapping("pagewluserwuliu")
    public String pageWlUserWuLiu(@RequestBody JSONObject jo){
        String wuLiuNo = jo.getString("wuLiuNo");
        this.pageSet(jo);
        List<IraorderwuliuVo> iraorderwuliuList = iraorderwuliuService.listIraorderwuliuByWlUserId(wuLiuNo,this.getUserId());
        for (IraorderwuliuVo iraorderwuliuVo : iraorderwuliuList) {
            List<Iraorderwuliustepspeed> iraorderwuliustepspeedList = iraorderwuliustepspeedService.listIraorderwuliustepspeed(iraorderwuliuVo.getId());
            iraorderwuliuVo.setStepList(iraorderwuliustepspeedList);
            List<IraOrderWuLiuProductVo> iraOrderWuLiuProductVoList  = iraorderwuliuproductService.listIraorderwuliuproduct(iraorderwuliuVo.getOrderid(), iraorderwuliuVo.getId());
            iraorderwuliuVo.setProductList(iraOrderWuLiuProductVoList);
        }
        PageInfo<IraorderwuliuVo> IraorderwuliuVoPageInfo = this.getPageInfo(iraorderwuliuList);
        return this.success(IraorderwuliuVoPageInfo);
    }
}
